$(function () {
	$("#jqGrid").jqGrid({
		url: '../tpurviewcustprotocol/list',
		datatype: "json",
		colModel: [			
		{ label: '公司名称', name: 'cropname', index: 'cropname', width: 80 }, 			
		{ label: '激活日期', name: 'begindate', index: 'begindate', width: 80 }, 			
		{ label: '到期日期', name: 'enddate', index: 'enddate', width: 80 }, 			
		{ label: '创建人', name: 'createname', index: 'createname', width: 80 }, 			
		{ label: '创建日期', name: 'createdate', index: 'createdate', width: 80 }, 			
		{ label: '编码', name: 'number', index: 'number', width: 80 }, 			
		{ label: '状态', name: 'status', width: 80, formatter: function(value, options, row){
			return value == 0 ? 
				'<span class="label label-success">正常</span>':
				'<span class="label label-danger">禁用</span>' ;
		}},			
		// { label: '客户信息id', name: 'custid', index: 'custid', width: 80 }, 			
		// { label: '续费', name:'id',search:false,index:'id', width:30,sortable: false, formatter: addFeeCell},	
		],
		viewrecords: true,
		height: 385,
		rowNum: 10,
		rowList : [10,30,50],
		rownumbers: true, 
		rownumWidth: 25, 
		autowidth:true,
		multiselect: true,
		pager: "#jqGridPager",
		jsonReader : {
			root: "page.list",
			page: "page.currPage",
			total: "page.totalPage",
			records: "page.totalCount"
		},
		prmNames : {
			page:"page", 
			rows:"limit", 
			order: "order"
		},
		gridComplete:function(){
    	//隐藏grid底部滚动条
    	$("#jqGrid").closest(".ui-jqgrid-bdiv").css({ "overflow-x" : "hidden" }); 
    }
  });
  // 续费
  function addFeeCell(cellValue, options, rowdata, action){
    return "<span onclick='addFee("+cellValue+")'></span>";
  }

  // 续费
  function addFee(rowId){
    console.log('addFee:'+rowId)
  }

});

var vm = new Vue({
	el:'#rrapp',
	data:{
		showList: true,
		title: null,
		tPurviewCustprotocol: {},
		// 新增，选择公司名称
		options: []
	},
	methods: {
		query: function () {
			vm.reload();
		},
		add: function(){
			vm.showList = false;
			vm.title = "新增";
			vm.tPurviewCustprotocol = {};
		},
		update: function (event) {
			var id = getSelectedRow();
			if(id == null){
				return ;
			}
			vm.showList = false;
			vm.title = "修改";

			vm.getInfo(id)
		},
		saveOrUpdate: function (event) {
			console.log(vm.tPurviewCustprotocol.custid);
			var legalObj = {
				number: true,
				custid: true,
				begindate: true,
				enddate: true,
			};
			// 非空校验
			if(!oneRowIsLegal(vm.tPurviewCustprotocol,legalObj)){
				alert('请先输入必填项');
				return;
			}
			var url = vm.tPurviewCustprotocol.id == null ? "../tpurviewcustprotocol/save" : "../tpurviewcustprotocol/update";
			$.ajax({
				type: "POST",
				url: url,
				contentType: "application/json",
				data: JSON.stringify(vm.tPurviewCustprotocol),
				success: function(r){
					if(r.code === 0){
						alert('操作成功', function(index){
							vm.reload();
						});
					}else{
						alert(r.msg);
					}
				}
			});
		},
		del: function (event) {
			var ids = getSelectedRows();
			if(ids == null){
				return ;
			}
			
			confirm('确定要删除选中的记录？', function(){
				$.ajax({
					type: "POST",
					url: "../tpurviewcustprotocol/delete",
					contentType: "application/json",
					data: JSON.stringify(ids),
					success: function(r){
						if(r.code == 0){
							alert('操作成功', function(index){
								$("#jqGrid").trigger("reloadGrid");
							});
						}else{
							alert(r.msg);
						}
					}
				});
			});
		},
		getInfo: function(id){
			$.get("../tpurviewcustprotocol/info/"+id, function(r){
				// 处理时间
				r.tPurviewCustprotocol.begindate = dateInputFormatter(r.tPurviewCustprotocol.begindate);
				r.tPurviewCustprotocol.enddate = dateInputFormatter(r.tPurviewCustprotocol.enddate);
				vm.tPurviewCustprotocol = r.tPurviewCustprotocol;
			});
		},
		reload: function (event) {
			vm.showList = true;
			var page = $("#jqGrid").jqGrid('getGridParam','page');
			$("#jqGrid").jqGrid('setGridParam',{ 
				page:page
			}).trigger("reloadGrid");
		},
		getOptions: function(){
			var that = this;
			// this.options = [
			// 	{
			// 		id: '001',
			// 		cropname: '公司1'
			// 	},
			// 	{
			// 		id: '002',
			// 		cropname: '公司2'
			// 	},
			// 	{
			// 		id: '003',
			// 		cropname: '公司3'
			// 	}
			// ];
			var url = "../tpurviewcustinfo/custinfolist";
			$.ajax({
				type: "GET",
				url: url,
				// contentType: "application/json",
				// data: JSON.stringify(vm.tPurviewCustprotocol),
				success: function(r){
					console.log(r);
						that.options = r.data || r || [];
				}
			});
		}
	},
	created: function(){
		this.getOptions();
	}
});